package com.tomato.displaySample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Spinner;
import android.widget.Toast;

public class Motor_display extends Activity {
	EditText motor_bottomDataValue7,motor_bottomDataValue8;
	Button motor_nextbutton,
			motor_sequanceButton,
			motor_controlSetButton,
			motor_quitButton,
			motor_bottomDataChanelNumButton;
	
	Spinner motor_numberOfControllerSpiner;
	CheckBox motor_checkBox1;
	int currentPositionOfController = 0;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.motorview);
		
		ArrayAdapter<String> controller = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		controller.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		controller.add("選択してください。");
		controller.add("1	コントローラA");
		controller.add("3	コントローラB");
		controller.add("10	コントローラC");
		controller.add("30	テスト");
		controller.add("40	デモ用");
		controller.add("100	A");
		controller.add("128	B");
		// TODO Auto-generated method stub
		
		motor_bottomDataChanelNumButton = (Button)findViewById(R.id.motor_bottomDataChanelNumButton);
		motor_nextbutton = (Button)findViewById(R.id.motor_nextbutton);
		motor_sequanceButton = (Button)findViewById(R.id.motor_sequanceButton);
		motor_controlSetButton = (Button)findViewById(R.id.motor_controlSetButton);
		motor_quitButton = (Button)findViewById(R.id.motor_quitButton);
		
		motor_numberOfControllerSpiner = (Spinner)findViewById(R.id.motor_numberOfControllerSpiner);
		motor_numberOfControllerSpiner.setAdapter(controller);
		motor_numberOfControllerSpiner.setSelection(0, false);
		
		motor_bottomDataValue7 = (EditText)findViewById(R.id.motor_bottomDataValue7);
		motor_bottomDataValue8 = (EditText)findViewById(R.id.motor_bottomDataValue8);
		motor_bottomDataValue7.setEnabled(false);
		motor_bottomDataValue8.setEnabled(false);
		
		motor_checkBox1 = (CheckBox)findViewById(R.id.motor_checkBox1);
		
		motor_bottomDataChanelNumButton.setOnClickListener(button_Listener);
		motor_nextbutton.setOnClickListener(button_Listener);
		motor_sequanceButton.setOnClickListener(button_Listener);
		motor_controlSetButton.setOnClickListener(button_Listener);
		motor_quitButton.setOnClickListener(button_Listener);
		motor_numberOfControllerSpiner.setOnItemSelectedListener(spiner_Listener);
		motor_checkBox1.setOnCheckedChangeListener(checkbox_Listener);
	}

	private OnClickListener button_Listener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.motor_nextbutton:
			{
				Intent intent=new Intent(Motor_display.this, DIO_display.class);
				intent.putExtra("controllerNum", currentPositionOfController);
				startActivity(intent);
				break;
			}
			case R.id.motor_sequanceButton:
			{	
				Intent intent=new Intent(Motor_display.this, sequance_display.class);
				startActivity(intent);
				break;
			}
			case R.id.motor_controlSetButton:		{
				Intent intent=new Intent(Motor_display.this, Control_display.class);
				startActivity(intent);
				break;
			}
			case R.id.motor_bottomDataChanelNumButton:
			{
				PopupMenu popupMenu = new PopupMenu(Motor_display.this, findViewById(R.id.motor_bottomDataChanelNumButton));
				popupMenu.getMenuInflater().inflate(R.menu.motor_popupnum,popupMenu.getMenu());
				 popupMenu.show();
			    // ポップアップメニューのメニュー項目のクリック処理
				 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
					 @Override
					 public boolean onMenuItemClick(MenuItem item) {
			            // 押されたメニュー項目名をToastで表示
						 
						 motor_bottomDataChanelNumButton.setText(item.getTitle());
			            Toast.makeText(Motor_display.this, item.getTitle(),  Toast.LENGTH_SHORT).show();
			            return true;
			        }
				 });
				break;
					
			}
			case R.id.motor_quitButton:
			{
				finish();
			}
			}
		}
	};
	
	private OnItemSelectedListener spiner_Listener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
			// TODO Auto-generated method stub
			currentPositionOfController = position;
			PopupMenu popupMenu = new PopupMenu(Motor_display.this, view);
			 popupMenu.getMenuInflater().inflate(R.menu.motor_popup,popupMenu.getMenu());
			 popupMenu.show();
		    // ポップアップメニューのメニュー項目のクリック処理
			 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
				 @Override
				 public boolean onMenuItemClick(MenuItem item) {
		            // 押されたメニュー項目名をToastで表示
		            Toast.makeText(Motor_display.this, item.getTitle(),  Toast.LENGTH_SHORT).show();
		            return true;
		        }
			 });
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
		
	};
	private OnCheckedChangeListener checkbox_Listener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			if(isChecked){
				motor_bottomDataValue7.setEnabled(true);
				motor_bottomDataValue7.setFocusableInTouchMode(true);
				motor_bottomDataValue8.setEnabled(true);
				motor_bottomDataValue8.setFocusableInTouchMode(true);
				
			}else{
				motor_bottomDataValue7.setEnabled(false);
				motor_bottomDataValue8.setEnabled(false);
				
			}
			
		}
	};
}
